package com.gdit.zax.servlet;

import java.io.IOException;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.gdit.zax.Constant;
import com.gdit.zax.dao.GroupDao;
import com.gdit.zax.entitys.GroupAll;
import com.gdit.zax.factory.DAOFactory;
import com.gdit.zax.utils.DBUtil;
import com.gdit.zax.utils.O;
import com.gdit.zax.utils.TextUtil;
import com.gdit.zax.utils.VeriFicUtil;

public class GetTScdule extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String accesstoken = request.getParameter("accesstoken");
		int page = TextUtil.String2int(request.getParameter("page"), 1);
		
		
		//检测参数
		if (TextUtil.isEmpty(accesstoken)) {
			O.printFail(response, 1, "请检查accesstoken参数!");
			return;
		}
		if (page < 0) {
			O.printFail(response, 1, "请检查page参数!");
			return;
		}
		
		
		//检测连接、用户
		int uid = VeriFicUtil.getVFID(accesstoken);
		Connection conn = DBUtil.getConnection();
		
		if (uid==Integer.MIN_VALUE || uid==Integer.MAX_VALUE) {
			O.printNoLogin(response);
			return;
		}
		
		if (conn==null) {
			O.printFail(response, 1000, "数据库连接失败!");
			return;
		}
		
		//开始
		DAOFactory dao  = DAOFactory.init(conn, request, response);
		GroupDao gdao = dao.getGroupDao();
		List<GroupAll> list =  gdao.getMyGroup(uid, page, 10);
		
//		cid
//		pic
//		classname
//		calssmembersum
//		teachername

		ArrayList<Map<String, Object>> data = new ArrayList<Map<String,Object>>();
		for (GroupAll groupAll : list) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("cid", groupAll.getT_group_id());
			map.put("pic", Constant.WEBRoot +  groupAll.getT_group_pic());
			map.put("classname", groupAll.getT_group_name());
			map.put("classmembersum", gdao.getGroupSubSum(uid));
			map.put("teachername", groupAll.getT_user_nick());
		}
		O.printSuccess(response, data);
	}

}
